package com.yksj.healthtalk.adapter;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.WeatherEntity;
import com.yksj.healthtalk.ui.R;

public class LocationExpandAdapter extends BaseExpandableListAdapter{

    private final Context mContext;
    private  LayoutInflater mInflater = null;
  	private WeatherEntity Object;
    private Map<WeatherEntity,List<WeatherEntity>>   mData = null;
    private Iterator iterator;
    private Map<Integer,WeatherEntity > map;
	 onChildItem itemin;
	private  WeatherEntity childEntity;

    public LocationExpandAdapter(Context ctx, Map<WeatherEntity,List<WeatherEntity>> list) {
        mContext = ctx;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mData = list;
        iterator = mData.keySet().iterator();
        map = new LinkedHashMap<Integer, WeatherEntity>();
    }


    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(map.get(groupPosition)).size();
    }

    @Override
    public List<WeatherEntity> getGroup(int groupPosition) {
        return mData.get(map.get(groupPosition));
    }

    @Override
    public WeatherEntity getChild(int groupPosition, int childPosition) {
        return mData.get(map.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
    	if (map.get(groupPosition) == null) {
    	 if(iterator.hasNext()){
         	Object  = (WeatherEntity) iterator.next();
         	map.put(groupPosition, Object);
			}
         }
    	 GroupViewHolder groupViewHolder;
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.location_group_item_layout, null);
            groupViewHolder= new GroupViewHolder();
            groupViewHolder.mGroupName = (TextView)convertView.findViewById(R.id.group_name);
            groupViewHolder.mGroupPic=(ImageView)convertView.findViewById(R.id.group_pic);
            convertView.setTag(groupViewHolder);
        }else {
        	groupViewHolder = (GroupViewHolder) convertView.getTag();
		}
        if(isExpanded){
        	groupViewHolder.mGroupPic.setBackgroundResource(R.drawable.arrows_down_city);
        }else{
        	groupViewHolder.mGroupPic.setBackgroundResource(R.drawable.arrows);
        }
        groupViewHolder.mGroupName.setText(map.get(groupPosition).getAREA_NAME());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
    	childEntity=mData.get(map.get(groupPosition)).get(childPosition);
    	ChildViewHolder holder;
        if (convertView == null) {
        	holder=new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.location_child_item_layout, null);
            holder.mChildName = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }else{
        	holder=(ChildViewHolder)convertView.getTag();
        }
        holder.mChildName.setText(childEntity.getAREA_NAME());
        
        convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				itemin.onChildItemClick(mData.get(map.get(groupPosition)).get(childPosition));
			}
		});
        
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        /*很重要：实现ChildView点击事件，必须返回true*/
        return true;
    }

    private static class GroupViewHolder {
        TextView mGroupName;
        ImageView mGroupPic;
    }

    private static class ChildViewHolder {
        TextView mChildName;
    }

	public void setIterator(Iterator<WeatherEntity> iterator2) {
		this.iterator = iterator2;
		notifyDataSetChanged();
	}

	public interface onChildItem{
		void onChildItemClick(WeatherEntity entity);
	}
	
	public void setOnChildItemClick(onChildItem item){
		this.itemin=item;
	}
	
}
